<template>
  <el-card class="table-card" shadow="never">
    <template #header>
      <div class="table-header">
        <span>{{ title }}</span>
        <div class="table-info">共 {{ total }} 条记录</div>
      </div>
    </template>

    <el-table
      v-loading="loading"
      :data="data"
      stripe
      style="width: 100%"
      v-bind="$attrs"
    >
      <slot />
    </el-table>

    <!-- 分页 -->
    <div class="pagination-wrapper" v-if="showPagination">
      <el-pagination
        :current-page="currentPage"
        :page-size="pageSize"
        :total="total"
        :page-sizes="pageSizes"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </el-card>
</template>

<script setup>
import { PAGINATION_SIZES } from "@/utils/constants";

const props = defineProps({
  title: {
    type: String,
    default: "数据列表",
  },
  data: {
    type: Array,
    default: () => [],
  },
  loading: {
    type: Boolean,
    default: false,
  },
  total: {
    type: Number,
    default: 0,
  },
  currentPage: {
    type: Number,
    default: 1,
  },
  pageSize: {
    type: Number,
    default: 10,
  },
  showPagination: {
    type: Boolean,
    default: true,
  },
  pageSizes: {
    type: Array,
    default: () => PAGINATION_SIZES,
  },
});

const emit = defineEmits([
  "update:currentPage",
  "update:pageSize",
  "size-change",
  "current-change",
]);

const handleSizeChange = (size) => {
  emit("update:pageSize", size);
  emit("size-change", size);
};

const handleCurrentChange = (page) => {
  emit("update:currentPage", page);
  emit("current-change", page);
};
</script>

<style lang="scss" scoped>
.table-card {
  flex: 1;
  display: flex;
  flex-direction: column;

  :deep(.el-card__body) {
    padding: 20px;
    flex: 1;
    display: flex;
    flex-direction: column;
  }

  .table-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 16px;

    span {
      font-weight: 600;
      color: $text-primary;
      font-size: 16px;
    }

    .table-info {
      color: $text-secondary;
      font-size: 14px;
    }
  }

  .pagination-wrapper {
    margin-top: 20px;
    display: flex;
    justify-content: center;
  }
}

:deep(.el-table) {
  width: 100% !important;
  flex: 1;
}
</style>
